package org.wu.framework.lazy.orm.core.source.mysql;

import lombok.Data;
import org.wu.framework.core.NormalUsedString;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.lazy.orm.core.persistence.conf.LazyTableFieldEndpoint;
import org.wu.framework.lazy.orm.core.persistence.conf.field.AbstractLazyTableFieldEndpoint;
import org.wu.framework.lazy.orm.core.stereotype.LazyTableField;

import static org.wu.framework.lazy.orm.core.source.mysql.MySQLSourceAdvancedTargetAbstract.specialFunction;

/**
 * mysql Java字段转换成mysql column对象处理
 */
@Data
public class MySQLColumnEndpoint extends AbstractLazyTableFieldEndpoint {
    /**
     * 数据库列类型
     * @return
     */
    @Override
    public String getColumnType() {
        return ObjectUtils.isEmpty(super.getColumnType()) ? LazyTableField.FieldType.getMysqlTypeByClass(super.getClazz()) : super.getColumnType();
    }

    /**
     * @return 行sql
     * description 创建SQL column
     * "`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',\n" +
     * @author Jiawei Wu
     * @date 2020/12/31 9:19 下午
     **/
    @Override
    public String columnSQL() {

        String extra = getExtra();
        if (ObjectUtils.isNotEmpty(extra) && (extra.startsWith("DEFAULT_GENERATED") || extra.startsWith("default_generated"))) {
            String extraUpperCase = extra.toUpperCase();
            extra = extraUpperCase.replaceAll("DEFAULT_GENERATED", "");
        }
        String oldDefaultValue = getDefaultValue();
        String defaultValue = oldDefaultValue;
        if (ObjectUtils.isNotEmpty(defaultValue) ) {
            if(defaultValue.startsWith(NormalUsedString.SINGLE_QUOTE)){
                //
            }else if( specialFunction.stream().noneMatch(functionString -> functionString.equalsIgnoreCase(oldDefaultValue))){
                defaultValue=NormalUsedString.SINGLE_QUOTE+defaultValue+NormalUsedString.SINGLE_QUOTE;
            }
        }
        String columnName = getColumnName();
        return

                (columnName.startsWith(NormalUsedString.BACKTICK)?columnName + NormalUsedString.SPACE : NormalUsedString.BACKTICK + getColumnName() + NormalUsedString.BACKTICK + NormalUsedString.SPACE )+
                getColumnType() + NormalUsedString.SPACE +
                (
                        isNotNull() ?
                                NormalUsedString.SPACE + NormalUsedString.NOT + NormalUsedString.SPACE + NormalUsedString.NULL + NormalUsedString.SPACE :
                                NormalUsedString.SPACE
                ) +
                (
                        ObjectUtils.isEmpty(defaultValue) ?
                                NormalUsedString.SPACE :
                                NormalUsedString.SPACE + NormalUsedString.DEFAULT + NormalUsedString.SPACE + defaultValue + NormalUsedString.SPACE
                ) +
                NormalUsedString.SPACE + extra + NormalUsedString.SPACE +
                NormalUsedString.COMMENT + NormalUsedString.SPACE +
                NormalUsedString.SINGLE_QUOTE + getComment() + NormalUsedString.SINGLE_QUOTE;
    }

    @Override
    public LazyTableFieldEndpoint clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return super.toString();
    }
}
